/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.actions;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.LinkedHashSet;
import java.util.Set;
import javax.swing.JOptionPane;
import org.opu.vcforms.io.Logger;
import org.opu.vcforms.resource.ResourceHandle;
import org.opu.vcforms.view.component.GUIProperties;

/**
 *
 * @author yura
 */
public class Exit extends AbstractAction {
    /** My name */
    public static final String MNAME = "Exit";
    private Set<ActionListener> listeners ;

    public Exit() {
        this(new LinkedHashSet<ActionListener>());
    }

    public Exit(Set<ActionListener> listeners) {
        this.listeners = listeners;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String name = getName() + "." + "Text";

        String yes = boundle.getString(name + ".yes");
        String no = boundle.getString(name + ".no");
        Object[] options = {yes, no};
        
        String appName = ResourceHandle.getProperty(ResourceHandle.APPLICATION_NAME);
        String msg = MessageFormat.format(boundle.getString(name), appName);

        String ttl = appName + " - " + getValue(SHORT_DESCRIPTION);
        
        int i = JOptionPane.showOptionDialog(null, msg, ttl,
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
                null, options, options[0]);

        if (i == JOptionPane.YES_OPTION) {
            for (ActionListener a : listeners) {
                a.actionPerformed(e);
            }
            
            GUIProperties gui = GUIProperties.instance();
            try {
                gui.store();
            } catch (IOException ex) {
                Logger.error(getClass().getName() + ": actionPerformed: "
                        + "In class " + gui.getClass().getName() + ": "
                        + "When store properties - " + ex);
            }

            System.exit(0);
        }
    }


    @Override
    public String getName(){
        return MNAME;
    }
}
